package com.berwin.bmfb.views.dialogs;

import com.berwin.bmfb.entity.BMChar;
import com.berwin.bmfb.entity.BMFont;
import com.berwin.bmfb.views.MainView;
import com.berwin.bmfb.views.components.DrawLetterView;
import com.berwin.bmfb.views.components.Toast;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class AddLetterDialog extends JDialog {

    private DrawLetterView drawLetterView = null;
    private JTextField tfLetterID = null;
    private JTextField tfLetter = null;
    private MainView owner;
    private BMFont bmFont;
    private BMChar bmChar;

    public AddLetterDialog(MainView owner, String title, BMFont bmFont, BMChar bmChar) {
        super(owner, title);
        this.owner = owner;
        this.bmFont = bmFont;
        this.bmChar = bmChar;
        this.setSize(new Dimension((int) (owner.getWidth() * 0.5f), (int) (owner.getHeight() * 0.4f)));
        this.setModal(true);
        this.setLayout(new BorderLayout());
        this.setLocationRelativeTo(owner);

        drawLetterView = new DrawLetterView(bmChar);
        drawLetterView.setNeedCenter(true);
        this.add(drawLetterView, BorderLayout.CENTER);
        drawLetterView.updateBMChar(bmChar);

        JPanel south = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        this.add(south, BorderLayout.SOUTH);

        south.add(new JLabel("ASCII:"));
        tfLetterID = new JTextField(8);
        south.add(tfLetterID);
        tfLetterID.setEnabled(false);

        south.add(new JLabel("图片字符:"));
        tfLetter = new JTextField(8);
        south.add(tfLetter);
        tfLetter.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                // 限制长度为1
                String text = tfLetter.getText();
                if (text.length() <= 0)
                    return;
                tfLetterID.setText(text.charAt(0) + 0 + "");
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                // 限制长度为1
                String text = tfLetter.getText();
                if (text.length() <= 0)
                    return;
                tfLetterID.setText(text.charAt(0) + 0 + "");
            }

            @Override
            public void changedUpdate(DocumentEvent e) {

            }
        });
        tfLetter.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    AddLetterDialog.this.save();
                }
            }
        });

        JButton btnCancel = new JButton("取消");
        south.add(btnCancel);
        btnCancel.addActionListener(e -> {
            this.dispose();
        });

        JButton btnSave = new JButton("保存");
        south.add(btnSave);
        btnSave.addActionListener(e -> {
            this.save();
        });


    }

    private void save() {
        String text = tfLetter.getText();
        if (text.length() <= 0) {
            owner.getToast().popup("字符不能为空", Toast.error);
            return;
        }
        bmChar.setId(text.charAt(0) + 0);
        bmChar.setLetter(text.substring(0, 1));
        bmFont.getBmChars().add(bmChar);
        owner.handleDoLayoutChars();
        bmFont.setLineHeight(Math.max(bmFont.getLineHeight(), bmChar.getHeight()));
        bmFont.setSize(bmChar.getHeight());
        owner.handleBMFontUpdated(bmFont);
        // 这里也可以更新图片的大小
//        Rectangle rect = bmFont.getRect();
//        bmFont.setScaleW(rect.width);
//        bmFont.setScaleH(rect.height);
        this.dispose();
    }

}
